package com.walker.service.catalogue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.walker.util.CommonUtils;

@Service
public class CatalogueServicesService {

	@Autowired
	@Qualifier("dmJdbcTemplate")
	private JdbcTemplate dmJdbcTemplate;
	
	/**
	 * 服务列表
	 * @param params
	 * @return
	 */
	public Map<String,Object> queryServieList(Map<String,Object> params){
		String keyword = CommonUtils.valueOf(params.get("keyword"));
		String group_id = CommonUtils.valueOf(params.get("group_id"));
		int pageSize = Integer.parseInt(CommonUtils.valueOf(params.get("pageSize")));
		int pageNo = Integer.parseInt(CommonUtils.valueOf(params.get("pageNo")));
		int offset = (pageNo - 1) * pageSize ;
		
		List<Object> paramList = new ArrayList<Object>();
		
		String countSql = getServiceCountSql(paramList,group_id,keyword);
		Object totalRecords = dmJdbcTemplate.queryForMap(countSql,paramList.toArray()).get("count");
		
		String sql = getServiceSql(group_id,keyword);
		paramList.add(pageSize);
		paramList.add(offset);
		List<Map<String,Object>> deriList = dmJdbcTemplate.queryForList(sql,paramList.toArray());
		
		Map<String,Object> data = new HashMap<String,Object>();
		data.put("total_records", totalRecords);
		data.put("service_data", deriList);
		
		return data;
	}

	/**
	 * 分组信息
	 * @return
	 */
	public List<Map<String,Object>> queryGroupInfo(){
		String sql = "select id as group_id,group_name from t_serv_api_group";
		return dmJdbcTemplate.queryForList(sql);
	}
	
	/**
	 * 服务明细
	 * @param api_id
	 * @return
	 */
	public Map<String,Object> queryServieDetail(String api_id){
		//请求参数
		String sql = "select api_id,param_name,param_type,is_require,demo_value,remark from t_serv_api_param where api_id = ? and param_class = 1";
		List<Map<String,Object>> request_param = dmJdbcTemplate.queryForList(sql,api_id);
		
		//返回参数
		sql = "select param_name,param_type,remark,demo_value from t_serv_api_param where api_id = ? and param_class = 0";
		List<Map<String,Object>> respone_param = dmJdbcTemplate.queryForList(sql,api_id);
		
		//目前是定死值后期待改造
		List<Map<String, Object>> common_param = new ArrayList<Map<String, Object>>();
		Map<String,Object> token=new HashMap<String,Object>();
		token.put("param_name", "auth-token");
		token.put("param_type", "STRING");
		token.put("is_require", "true");
		token.put("demo_value", "adhduawhjiaklasklajdklasjdkl");
		token.put("remark", "用户服务调用令牌");
		common_param.add(token);
		
		Map<String,Object> contentType=new HashMap<String,Object>();
		contentType.put("param_name", "content-type");
		contentType.put("param_type", "STRING");
		contentType.put("is_require", "true");
		contentType.put("demo_value", "application/json;charset=utf-8");
		contentType.put("remark", "内容类型");
		common_param.add(contentType);
		
		StringBuffer baseSql = new StringBuffer();
		baseSql.append("select a.id as api_id,");
		baseSql.append("       api_name,");
		baseSql.append("       b.group_name,");
		baseSql.append("       a.api_remark,");
		baseSql.append("       a.api_group,");
		baseSql.append("       a.publish_person,");
		baseSql.append("       a.publish_time,");
		baseSql.append("       (select count(distinct c.app_id) app_num from t_serv_api_approve c where a.id = c.api_id and c.flag = 1) as app_num,");
		baseSql.append("       0 as call_num,");
		baseSql.append("       a.api_path,");
		baseSql.append("       a.api_request_type,");
		baseSql.append("       a.api_protocol,");
		baseSql.append("       a.api_reponse_type,");
		baseSql.append("       a.sql_template,");
		baseSql.append("       a.update_time");
		baseSql.append("  from t_serv_api_base a");
		baseSql.append("  left join t_serv_api_group b");
		baseSql.append("    on a.api_group = b.id");
		baseSql.append(" where a.id = ?");
		Map<String,Object> baseInfo =  dmJdbcTemplate.queryForMap(baseSql.toString(),api_id);
		
		Map<String,Object> data = new HashMap<String,Object>();
		data.put("request_param", request_param);
		data.put("respone_param", respone_param);
		data.put("common_param", common_param);
		data.put("basic_data", baseInfo);
		return data;
	}
	
	private String getServiceSql(String group_id,String keyword) {
		StringBuffer sql = new StringBuffer();
		sql.append("select a.id as api_id,");
		sql.append("       api_name,");
		sql.append("       b.group_name,");
		sql.append("       a.api_remark,");
		sql.append("       a.api_group,");
		sql.append("       a.publish_person,");
		sql.append("       a.publish_time,");
		sql.append("       (select count(distinct c.app_id) app_num from t_serv_api_approve c where a.id = c.api_id and c.flag = 1) as app_num,");
		sql.append("       0 as call_num,");
		sql.append("       a.api_path,");
		sql.append("       a.api_request_type,");
		sql.append("       a.api_protocol,");
		sql.append("       a.api_reponse_type,");
		sql.append("       a.sql_template,");
		sql.append("       a.update_time");
		sql.append("  from t_serv_api_base a");
		sql.append("  left join t_serv_api_group b");
		sql.append("    on a.api_group = b.id");
		sql.append(" where a.status = 1");
		if(StringUtils.isNotBlank(group_id)){
			sql.append(" and b.id = ?");
		}
		if(StringUtils.isNotBlank(keyword)){
			sql.append(" and (a.api_name like ? or a.api_path like ?)");
		}
		sql.append("  order by a.publish_time desc");
		sql.append(" limit ? offset ? ");
		return sql.toString();
	}
	
	private String getServiceCountSql(List<Object> paramList,String group_id,String keyword) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(1) count from t_serv_api_base where status = 1");
		if(StringUtils.isNotBlank(group_id)){
			sql.append(" and api_group = ?");
			paramList.add(group_id);
		}
		if(StringUtils.isNotBlank(keyword)){
			sql.append(" and (api_name like ? or api_path like ?)");
			paramList.add("%"+keyword+"%");
			paramList.add("%"+keyword+"%");
		}
		return sql.toString();
	}
}